import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

public class FreeMarkerExample {
    public static void main(String[] args) throws Exception {
        // 1. 配置 FreeMarker
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);
        cfg.setClassForTemplateLoading(FreeMarkerExample.class, "/templates");
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        // 2. 构造数据模型
        Map<String, Object> dataModel = new HashMap<>();
        dataModel.put("user", "Alice");
        dataModel.put("items", new String[]{"Apple", "Banana", "Cherry"});

        // 3. 加载模板
        Template template = cfg.getTemplate("hello.ftl");

        // 4. 渲染输出
        StringWriter out = new StringWriter();
        template.process(dataModel, out);

        System.out.println(out.toString());
    }
}
